home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1993 October: Windmill on DISC / ADC Developer CD (1993-10) (''Windmill On DISC'')_iso / Dev.CD Oct 93.iso / System Software / U.S. System Software / System 7 Pro™ Beta 11 / Development Tools / Sample Code / Messaging Service Access Module / Internet PMSAM / Internet PMSAM source / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-28  |  4.8 KB  |  240 lines  |  [TEXT/MPS ]

  1. /*-------------------------------------------------------------------
  2.  
  3. AOCE Post Office Protocol (POP) / Simple Mail Transfer Protocol (SMTP)
  4. Mail Service Access Module
  5.  
  6. written by Steve Falkenburg-- MacDTS
  7. ©1991-1993 Apple Computer, Inc.
  8.  
  9. --------------
  10. change history
  11. --------------
  12.  
  13. SJF        02/19/93    update for beta build    b1
  14. SJF        10/29/92    update to a11            a11
  15. SJF        06/08/92    update to a8            a8
  16. SJF        02/15/92    first working version    a4.5
  17. SJF        10/16/91    initial coding            a3
  18.  
  19. ---------------------------------------------------------------------*/
  20.  
  21. #ifndef __TYPES__
  22. #include <Types.h>
  23. #endif
  24.  
  25. #ifndef __MEMORY__
  26. #include <Memory.h>
  27. #endif
  28.  
  29. #ifndef __SEGLOAD__
  30. #include <SegLoad.h>
  31. #endif
  32.  
  33. #ifndef __TRAPS__
  34. #include <Traps.h>
  35. #endif
  36.  
  37. #ifndef __EVENTS__
  38. #include <Events.h>
  39. #endif
  40.  
  41. #ifndef __EPPC_
  42. #include <EPPC.h>
  43. #endif
  44.  
  45. #ifdef applec
  46. #include <SysEqu.h>
  47. #endif
  48.  
  49. #include "const.h"
  50. #include "gwerrors.h"
  51. #include "globals.h"
  52. #include "network.h"
  53. #include "gatewaystuff.h"
  54. #include "trapavailable.h"
  55. #include "utils.h"
  56. #include "myevents.h"
  57. #include "spoolsystem.h"
  58. #include "authstuff.h"
  59. #include "gatewayevents.h"
  60. #include "main.h"
  61.  
  62. #ifdef __SYSEQU__
  63. #define DefaultStackSize    *((long *)DefltStack)
  64. #define StackBase            *((Ptr *)CurStackBase)
  65. #else
  66. #define DefaultStackSize    DefltStack
  67. #define StackBase            CurStackBase
  68. #endif
  69.  
  70. // main
  71. //
  72. // main entry point- initializes program components and
  73. // calls main event loop.  exits via ExitToShell if gateway can't initialize
  74. //
  75. void main(void)
  76. {
  77.     OSErr err;
  78.     
  79.     TraceExecution("\pGATEWAY LAUNCH");
  80.     
  81.     if (!HasAOCE()) 
  82.         ExitToShell();
  83.         
  84.     err = InitMacStuff();
  85.     if (err!=noErr) {
  86.         DoError(err);
  87.         ExitToShell();
  88.     }
  89.         
  90.     err = InitSpoolSubsystem();
  91.     if (err!=noErr) {
  92.         DoError(err);
  93.         ExitToShell();
  94.     }
  95.  
  96.     err = InitRemoteNetStuff();
  97.     if (err!=noErr) {
  98.         DoError(err);
  99.         ExitToShell();
  100.     }
  101.         
  102.     err = InitAOCEStuff();
  103.     if (err!=noErr) {
  104.         DoError(err);
  105.         ExitToShell();
  106.     }
  107.         
  108.     err = InitGatewayStuff();
  109.     if (err!=noErr) {
  110.         DoError(err);
  111.         ExitToShell();
  112.     }
  113.         
  114.     MainLoop();
  115.     ExitProc();
  116. }
  117.  
  118.  
  119. // ExitProc
  120. //
  121. // called before the gateway terminates.  this call insures that everything associated
  122. // with AOCE and external networking stuff is closed and released.
  123. //
  124. void ExitProc(void)
  125. {
  126.     TraceExecution("\pGATEWAY QUIT");
  127.     
  128.     CloseGatewayStuff();
  129.     CloseRemoteNetStuff();
  130.     CloseAOCEStuff();
  131.     
  132.     ExitToShell();
  133. }    
  134.  
  135.  
  136. // InitMacStuff
  137. //
  138. // called to initialize the Mac toolbox stuff required for the gateway, which is a background
  139. // only application.
  140. //
  141. OSErr InitMacStuff(void)
  142. {
  143.     SetApplLimit((Ptr) ((long) StackBase - (long) 0x6000));    // workaround for DefltStackSize lomem being munged
  144.     MaxApplZone();
  145.     MoreMasters();
  146.     MoreMasters();
  147.     InitGraf(&qd.thePort);        // need this to generate random numbers
  148.     
  149.     gAOCEInited = false;
  150.     gDone = false;
  151.     gWakeUp = gWakeUpSecondary = false;
  152.     
  153.     if (!TrapAvailable(_WaitNextEvent))
  154.         return kCantRun;
  155.     
  156.     if (!SupportsAEVT())
  157.         return kCantRun;
  158.     
  159.     GetCurrentProcess(&gOurPSN);
  160.     
  161.     return noErr;
  162. }
  163.  
  164.  
  165. // MainLoop
  166. //
  167. // this is the main event loop for the gateway application.  the sleep time will be set to the
  168. // maximum value (-1L) so that we never take any idle time and only wake up when we're sent
  169. // an event.
  170. //
  171. // *** SPECIAL NOTE:
  172. //
  173. // it's not strictly necessary to have this event loop at all.  the toolbox will re-launch the
  174. // gateway whenenver it actually needs to have the gateway to something.  i've chosen to keep the
  175. // gateway running at all times, since i read in the slot information into a global area and also
  176. // to insure that i will always have enough memory to activate.  this is probably not the optimal
  177. // strategy, but it is only sample code...
  178. // 
  179. void MainLoop(void)
  180. {
  181.     EventRecord ev;
  182.     Boolean gotEvt;
  183.     OSErr err;
  184.     
  185.     while (gDone==false) {
  186.         
  187.         // see if we need to refresh slot info
  188.         err = CheckSlotRefresh();
  189.         if (err!=noErr)
  190.             DoError(err);
  191.         
  192.         // see if we need to refresh identity stuff (in response to auth q notification)    
  193.         err = CheckAuthRefresh();    
  194.         if (err!=noErr)
  195.             DoError(err);
  196.  
  197.         err = ProcessQueuedEvents();
  198.         if (err!=noErr)
  199.             DoError(err);
  200.         
  201.         gotEvt = false;
  202.         if (gWakeUp==false)
  203.             gotEvt = WaitNextEvent(everyEvent,&ev,kSleepTime,nil);
  204.         gWakeUp = false;
  205.         if (gotEvt) {
  206.             err = HandleEvent(&ev,true);
  207.             if (err!=noErr)
  208.                 DoError(err);
  209.         }
  210.     }
  211. }
  212.  
  213.  
  214.  
  215. // SecondaryEventHandler
  216. //
  217. // this event handler is what we spin on when waiting for an async call to complete
  218. // we will process a subset of the EPPCs from here, namely: CreateSlot, ModifySlot, DeleteSlot
  219. // all other EPPCs are queued onto a waiting to be processed event queue
  220. // (not re-entrant)
  221. //
  222. void SecondaryEventLoop(void)
  223. {
  224.     EventRecord ev;
  225.     Boolean gotEvt;
  226.     OSErr err;
  227.  
  228.     gotEvt = false;
  229.     
  230.     if (gWakeUpSecondary==false) {
  231.         gotEvt = WaitNextEvent(everyEvent,&ev,kSecondarySleepTime,nil);
  232.     }
  233.     
  234.     if (gotEvt) {
  235.         err = HandleEvent(&ev,false);
  236.         if (err!=noErr)
  237.             DoError(err);
  238.     }
  239. }
  240.